<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="计划编号" prop="trainCode">
						<el-input v-model="form.trainCode" placeholder="计划编号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="培训标题" prop="title">
						<el-input v-model="form.title" placeholder="培训标题" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			<el-col :lg="12">
				<el-form-item label="培训总人数" prop="userCount">
					<el-input-number v-model="form.userCount" placeholder="培训总人数"></el-input-number>
				</el-form-item>
			</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="培训渠道" prop="channel">
						<sc-select v-model="form.channel" :params="{code:'channelType'}" :apiObj="$API.hr.hrDic.getHrDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="培训类型" prop="courseType">
						<sc-select v-model="form.courseType" :params="{code:'courseType'}" :apiObj="$API.hr.hrDic.getHrDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="主办部门" prop="holdDept">
						<el-input v-model="form.holdDept" placeholder="主办部门" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="培训负者人" prop="chargePerson">
						<el-input v-model="form.chargePerson" placeholder="培训负者人" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="培训机构" prop="institutionName">
						<el-input v-model="form.institutionName" placeholder="培训机构" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="受训地址" prop="address">
						<el-input v-model="form.address" placeholder="受训地址" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="对方联系人" prop="institutionUser">
						<el-input v-model="form.institutionUser" placeholder="对方联系人" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="对方联系方式" prop="institutionContact">
						<el-input v-model="form.institutionContact" placeholder="对方联系方式" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="课程名称" prop="courseName">
						<el-input v-model="form.courseName" placeholder="课程名称" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="总课时" prop="courseTime">
						<el-input v-model="form.courseTime" placeholder="总课时" maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开课时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="开课时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="结束时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="培训经费" prop="founds">
						<el-input-number v-model="form.founds" placeholder="培训经费"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="审批人" prop="approvedUser">
						<select-user-dialog v-model:accountId="form.approvedUser" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="参与人" prop="joinUser">
				<select-user-dialog v-model:accountId="form.joinUser" :mode="true"/>
			</el-form-item>
			<el-form-item label="参与部门" prop="joinDept">
				<select-dept-dialog v-model:deptId="form.joinDept" :mode="true"/>
			</el-form-item>
			<el-form-item label="参与职务" prop="joinUserLevel">
				<select-user-level-dialog v-model:levelId="form.joinUserLevel" :mode="true"/>
			</el-form-item>
			<el-form-item label="培训摘要" prop="description">
				<el-input v-model="form.description" placeholder="培训摘要" maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="培训要求" prop="trainRequires">
				<el-input v-model="form.trainRequires" placeholder="培训要求" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="培训备注">
				<el-input v-model="form.remark" placeholder="培训备注" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'hr'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import AttachMenu from '@/views/module/attachMenu'
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		selectDeptDialog,
		selectUserLevelDialog,
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '添加培训记录',
				edit: '编辑培训记录',
				show: '查看'
			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			templateList: [],
			templates: [],
			form: {
				recordId: '',
				sortNo: '',
				trainCode: '',
				title: '',
				userCount:'',
				channel:'',
				courseType: '',
				holdDept: '',
				chargePerson: "",
				institutionName: "",
				address: "",
				institutionUser: "",
				institutionContact: "",
				courseName: "",
				courseTime: "",
				beginTime:"",
				endTime:"",
				founds:"",
				approvedUser:"",
				joinDept:"",
				joinUserLevel:"",
				description:"",
				trainRequires:"",
				attachId: '',
				remark: ''
			},
			rules: {
				trainCode:[
					{required: true, message: '计划编号不能为空'}
				],
				title: [
					{required: true, message: '培训标题不能为空'}
				],
				holdDept: [
					{required: true, message: '主办部门不能为空'}
				],
				userCount: [
					{required: true, message: '培训人数不能为空'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.hr.hrTrainRecord.insertHrTrainRecord.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.hr.hrTrainRecord.updateHrTrainRecord.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.sortNo = data.sortNo
			this.form.trainCode = data.trainCode
			this.form.title = data.title
			this.form.userCount = data.userCount
			this.form.channel = data.channel
			this.form.courseType = data.courseType
			this.form.holdDept = data.holdDept
			this.form.chargePerson = data.chargePerson
			this.form.institutionName = data.institutionName
			this.form.address = data.address
			this.form.institutionUser = data.institutionUser
			this.form.institutionContact = data.institutionContact
			this.form.courseName = data.courseName
			this.form.courseTime = data.courseTime
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.founds = data.founds
			this.form.approvedUser = data.approvedUser
			this.form.joinDept = data.joinDept
			this.form.joinUserLevel = data.joinUserLevel
			this.form.description = data.description
			this.form.trainRequires = data.trainRequires
			this.form.attachId = data.attachId
			this.form.remark = data.remark
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
